﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace StorageApp.Utility
{
    class Method
    {
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        public static string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase??"";
        public static string CONFIGINI = path + "config.txt";
        /// <summary>    
        /// 读取INI文件    
        /// </summary>    
        /// <param name="section">项目名称(如 [section] )</param>    
        /// <param name="skey">键</param>   
        /// <param name="path">路径</param> 
        public static string IniReadValue(string skey, string section = "soft")
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(section, skey, "", temp, 500, CONFIGINI);

            return temp.ToString();
        }
        //从TXT 读取文件
        /// <summary>
        /// 写入ini文件
        /// </summary>
        /// <param name="section">项目名称</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="path">路径</param>
        public static void IniWrite(string key, string value, string section = "soft")
        {
            WritePrivateProfileString(section, key, value, CONFIGINI);
        }



        //字符品转换成 DOUBLE
        public static double getNum(string num)
        {
            if (!string.IsNullOrEmpty(num))
            {
                double i01;
                bool b01 = double.TryParse(num, out i01);
                if (b01)
                {
                    return i01;
                }
            }
            return 0.0;
        }
    }
}
